package com.blb.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.blb.community.Utils.SnowflakeIdWorker;
import com.blb.community.mapper.ZyOwnerMapper;
import com.blb.community.mapper.ZyOwnerRoomMapper;
import com.blb.community.mapper.ZyOwnerRoomRecordMapper;
import com.blb.community.pojo.*;

import com.blb.community.service.ISysDictDataService;
import com.blb.community.service.IZyOwnerRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ZyOwnerRoomServiceImpl extends ServiceImpl<ZyOwnerRoomMapper, ZyOwnerRoom> implements IZyOwnerRoomService {
    @Resource
    private ZyOwnerRoomMapper zyOwnerRoomMapper;
    @Resource
    private ZyOwnerRoomRecordMapper zyOwnerRoomRecordMapper;
    @Resource
    private ZyOwnerMapper zyOwnerMapper;
    @Autowired
    private ISysDictDataService sysDictDataService;

    private SnowflakeIdWorker snowId = new SnowflakeIdWorker(3L, 17L);

    /**
     * 搜索结果加分页
     * @param selectOwnerRoom
     * @return
     */
    @Override
    public Page<ZyOwnerRoom> getOwnerRoomPage(SelectOwnerRoom selectOwnerRoom) {
        return zyOwnerRoomMapper.selectOwnerPage(new Page<>(selectOwnerRoom.getPage(),
                selectOwnerRoom.getSize()), selectOwnerRoom.getStatus());
    }

    /**
     * 修改审核记录
     * @param reqBody
     */
    @Transactional
    @Override
    public void updateOwnerRoom(ReqBody reqBody) {
        ZyOwnerRoom zor = reqBody.getZyOwnerRoom();
        zor.setUpdateTime(LocalDateTime.now());
        zyOwnerRoomMapper.update(zor, new QueryWrapper<ZyOwnerRoom>().lambda()
                .eq(ZyOwnerRoom::getOwnerRoomId, zor.getOwnerRoomId()));
        // 雪花算法获取id
        Long id = snowId.nextId();
        // 添加审核记录
        ZyOwnerRoomRecord zyOwnerRoomRecord = new ZyOwnerRoomRecord(id, zor.getOwnerRoomId() + "",
                zor.getCommunityId(), zor.getBuildingId(), zor.getUnitId(), zor.getRoomId(), zor.getOwnerId(),
                zor.getOwnerType(), zor.getRoomStatus(), reqBody.getAuditOpinion(), reqBody.getAuditType(),
                reqBody.getAuditId(), zor.getUpdateBy(), LocalDateTime.now());
        zyOwnerRoomRecordMapper.insert(zyOwnerRoomRecord);
    }

    /**
     * 获取审核记录的分页数据
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<ZyOwnerRoomRecord> getZyOwnerRoomRecord(Long page, Long size) {
        return zyOwnerRoomRecordMapper.selectPage(new Page<>(page, size));
    }

    @Transactional
    @Override
    public void addOwnerRoom(AddOwnerRoom addOwnerRoom) {
        zyOwnerMapper.updateZyOwnerMapperByRoom(addOwnerRoom);
        String room = addOwnerRoom.getRoomArr();
        String[] roomArr = room.split("-");
        ZyOwnerRoom zyOwnerRoom = new ZyOwnerRoom(snowId.nextId(), Long.parseLong(roomArr[0]),
                Long.parseLong(roomArr[1]), Long.parseLong(roomArr[2]), Long.parseLong(roomArr[3]),
                addOwnerRoom.getOwnerId(),addOwnerRoom.getHouseId(),addOwnerRoom.getOwnerType(),"Auditing",addOwnerRoom.getOwnerRealName(),
                LocalDateTime.now());
        zyOwnerRoomMapper.insert(zyOwnerRoom);
        ZyOwnerRoomRecord zyOwnerRoomRecord = new ZyOwnerRoomRecord(snowId.nextId(), zyOwnerRoom.getOwnerRoomId().toString(),
                zyOwnerRoom.getCommunityId(), zyOwnerRoom.getBuildingId(), zyOwnerRoom.getUnitId(), zyOwnerRoom.getRoomId(),
                zyOwnerRoom.getOwnerId(), zyOwnerRoom.getOwnerType(), zyOwnerRoom.getRoomStatus(), zyOwnerRoom.getOwnerId(),
                zyOwnerRoom.getCreateBy(), zyOwnerRoom.getCreateTime());
        zyOwnerRoomRecordMapper.insert(zyOwnerRoomRecord);
    }

    @Override
    public List<ZyOwnerRoomRecord> getZyOwnerRoomRecordByOwnerId(Long ownerId) {
        List<ZyOwnerRoomRecord> zyOwnerRoomRecords = zyOwnerRoomRecordMapper.selectListByOwnerId(ownerId);
        for (ZyOwnerRoomRecord zyOwnerRoomRecord : zyOwnerRoomRecords) {
            zyOwnerRoomRecord.setOwnerType(sysDictDataService.getByOwnerRoomKey(zyOwnerRoomRecord.getOwnerType()));
        }
        return zyOwnerRoomRecords;
    }

    @Override
    public void deleteById(Long id) {
        zyOwnerRoomRecordMapper.deleteByIdOfOwner(id);
    }

    @Override
    public List<ZyOwnerRoomRecord> getZyOwnerRoomRecordByBinding(Long ownerId) {
        List<ZyOwnerRoomRecord> zyOwnerRoomRecords = zyOwnerRoomRecordMapper.selectBindingByOwnerId(ownerId);
        for (ZyOwnerRoomRecord zyOwnerRoomRecord : zyOwnerRoomRecords) {
            zyOwnerRoomRecord.setOwnerType(sysDictDataService.getByOwnerRoomKey(zyOwnerRoomRecord.getOwnerType()));
        }
        return zyOwnerRoomRecords;
    }
}
